package com.ruoyi.peoplegroup.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.peoplegroup.domain.Partnerinfo;
import com.ruoyi.peoplegroup.domain.ResearcherinfoHistory;
import com.ruoyi.peoplegroup.service.IResearcherinfoHistoryService;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.peoplegroup.mapper.ResearcherinfoMapper;
import com.ruoyi.peoplegroup.domain.Researcherinfo;
import com.ruoyi.peoplegroup.service.IResearcherinfoService;

/**
 * 科研人员信息Service业务层处理
 * 
 * @author gw
 * @date 2025-01-08
 */
@Service
public class ResearcherinfoServiceImpl implements IResearcherinfoService 
{
    @Autowired
    private ResearcherinfoMapper researcherinfoMapper;
    @Autowired
    private IResearcherinfoHistoryService researcherinfoHistoryService;

    /**
     * 查询科研人员信息
     * 
     * @param researcherId 科研人员信息主键
     * @return 科研人员信息
     */
    @Override
    public Researcherinfo selectResearcherinfoByResearcherId(Long researcherId)
    {
        return researcherinfoMapper.selectResearcherinfoByResearcherId(researcherId);
    }

    /**
     * 查询科研人员信息列表
     * 
     * @param researcherinfo 科研人员信息
     * @return 科研人员信息
     */
    @Override
    public List<Researcherinfo> selectResearcherinfoList(Researcherinfo researcherinfo)
    {
        return researcherinfoMapper.selectResearcherinfoList(researcherinfo);
    }

    /**
     * 新增科研人员信息
     * 
     * @param researcherinfo 科研人员信息
     * @return 结果
     */
    @Override
    public int insertResearcherinfo(Researcherinfo researcherinfo)
    {
        researcherinfo.setCreatedBy(SecurityUtils.getUsername());
        ResearcherinfoHistory researcherinfohistory=new ResearcherinfoHistory();
        BeanUtils.copyProperties(researcherinfo,researcherinfohistory);
        researcherinfohistory.setOperationType("新增");
        researcherinfoHistoryService.insertResearcherinfoHistory(researcherinfohistory);
        int result=researcherinfoMapper.insertResearcherinfo(researcherinfo);

        return result;
    }

    /**
     * 修改科研人员信息
     * 
     * @param researcherinfo 科研人员信息
     * @return 结果
     */
    @Override
    public int updateResearcherinfo(Researcherinfo researcherinfo)
    {
        researcherinfo.setUpdatedBy(SecurityUtils.getUsername());
        ResearcherinfoHistory researcherinfohistory=new ResearcherinfoHistory();
        BeanUtils.copyProperties(researcherinfo,researcherinfohistory);
        researcherinfohistory.setOperationType("修改");
        researcherinfoHistoryService.insertResearcherinfoHistory(researcherinfohistory);
        int result=researcherinfoMapper.updateResearcherinfo(researcherinfo);

        return result;
    }

    /**
     * 批量删除科研人员信息
     * 
     * @param researcherIds 需要删除的科研人员信息主键
     * @return 结果
     */
    @Override
    public int deleteResearcherinfoByResearcherIds(Long[] researcherIds)
    {
        int updatedCount = 0;  // 计数器，记录成功更新的记录数

        for (Long id : researcherIds) {
            // 获取合作单位信息
            Researcherinfo researcherinfo= selectResearcherinfoByResearcherId(id);

            if (researcherinfo != null) {
                // 设置删除相关信息
                researcherinfo.setDeletedBy(SecurityUtils.getUsername());
                researcherinfo.setDeletedAt(new Date());
                researcherinfo.setIsDeleted(1);  // 标记为已删除

                // 更新合作单位信息
                int result = researcherinfoMapper.updateResearcherinfo(researcherinfo);
                if (result > 0) {
                    updatedCount++;  // 如果更新成功，计数器加1
                }
            }
        }

        return updatedCount;
    }

    /**
     * 删除科研人员信息信息
     * 
     * @param researcherId 科研人员信息主键
     * @return 结果
     */
    @Override
    public int deleteResearcherinfoByResearcherId(Long researcherId)
    {
        return researcherinfoMapper.deleteResearcherinfoByResearcherId(researcherId);
    }
}
